package com.virtualmall.controller;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
//import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.virtualmall.bean.Account;
import com.virtualmall.bean.Product;
import com.virtualmall.common.Constants;
import com.virtualmall.common.Page;
import com.virtualmall.service.ProductService;

@Controller
@RequestMapping("/product")
public class ProductController {

    //private Logger logger = Logger.getLogger(ProductController.class);

    @Autowired
    private ProductService productService;
    
    @RequestMapping(value="/create",method=RequestMethod.POST)
    @ResponseBody
    public Map<String,String> create(Product product,HttpServletRequest request){
        //logger.info("create() method begin...");

        Map<String,String> result = new HashMap<String,String>();
        
        try {
            Date nowDate = new Date();
            product.setCreateDate(nowDate);
            product.setUpdateDate(nowDate);
            
            Account account = new Account();
            account.setId((Long)request.getSession().getAttribute(Constants.USER_ID_SESSION));
            product.setAccount(account);
            
            this.productService.create(product);
            
            result.put("result", Constants.SUCCESS_RESULT_FLAG);
        } catch (Exception e) {
            //logger.error("create() method error...",e);
            
            result.put("result", Constants.FAILURE_RESULT_FLAG);
        }
        
        //logger.info("create() method end...");

        return result;
    }

    @RequestMapping(value="/update",method=RequestMethod.POST)
    @ResponseBody
    public Map<String,String> update(Product productModel,HttpServletRequest request){
        //logger.info("update() method begin...");

        Map<String,String> result = new HashMap<String,String>();
        
        try {
            Product product = this.productService.read(productModel.getId());
            
            product.setProductName(productModel.getProductName());
            product.setDiscountPrice(productModel.getDiscountPrice());
            product.setOriginalPrice(productModel.getOriginalPrice());
            product.setType(productModel.getType());
            product.setStock(productModel.getStock());
            product.setHot(productModel.getHot());
            product.setAuthor(productModel.getAuthor());
            product.setPublishingCompany(productModel.getPublishingCompany());
            product.setPublishingDate(productModel.getPublishingDate());
            product.setImagePath(productModel.getImagePath());
            product.setUpdateDate(new Date());
            
            this.productService.update(product);
            
            result.put("result", Constants.SUCCESS_RESULT_FLAG);
        } catch (Exception e) {
            //logger.error("update() method error...",e);
            
            result.put("result", Constants.FAILURE_RESULT_FLAG);
        }
        
        //logger.info("update() method end...");

        return result;
    }
    
    @RequestMapping(value="/list",method=RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> list(Page page){
        //logger.info("list() method begin...");

        Map<String,Object> result = new HashMap<String,Object>();
        
        try {
            List<Product> productList = this.productService.getProductList(null,page);
            Long productListCount = this.productService.getProductListCount(null);

            result.put("productList", productList);
            result.put("productListCount", productListCount);
            result.put("result", Constants.SUCCESS_RESULT_FLAG);
        } catch (Exception e) {
            //logger.error("list() method error...",e);
            
            result.put("result", Constants.FAILURE_RESULT_FLAG);
        }
        
        //logger.info("list() method end...");

        return result;
    }

    @RequestMapping(value="/search")
    public ModelAndView search(ModelMap modelMap,HttpServletRequest request){
        String keyword = request.getParameter("keyword");
        if(StringUtils.isNotEmpty(keyword)){
            try {
                modelMap.put("keyword", new String(keyword.getBytes("ISO8859-1"),"utf-8"));
            } catch (UnsupportedEncodingException e) {
                //logger.error("search() method error...",e);
            }
        }
        ModelAndView mav = new ModelAndView("/product/searchList.jsp", modelMap);
        
        return mav;
    }
    
    @RequestMapping(value="/searchProduct",method=RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> searchProduct(String keyword,Page page){
        //logger.info("search() searchProduct begin...");

        Map<String,Object> result = new HashMap<String,Object>();
        
        try {
            List<Product> productList = this.productService.getProductList(keyword,page);
            Long productListCount = this.productService.getProductListCount(keyword);

            result.put("productList", productList);
            result.put("productListCount", productListCount);
            result.put("result", Constants.SUCCESS_RESULT_FLAG);
        } catch (Exception e) {
            //logger.error("searchProduct() method error...",e);
            
            result.put("result", Constants.FAILURE_RESULT_FLAG);
        }
        
        //logger.info("searchProduct() method end...");

        return result;
    }
    
    @RequestMapping(value="/delete",method=RequestMethod.POST)
    @ResponseBody
    public Map<String,String> delete(Product product){
        //logger.info("delete() method begin...");

        Map<String,String> result = new HashMap<String,String>();
        
        try {
            this.productService.delete(product);
            
            result.put("result", Constants.SUCCESS_RESULT_FLAG);
        } catch (Exception e) {
            //logger.error("delete() method error...",e);
            
            result.put("result", Constants.FAILURE_RESULT_FLAG);
        }
        
        //logger.info("delete() method end...");

        return result;
    }
    
    @RequestMapping(value="/{productId}")
    public String productDetailInfo(@PathVariable(value="productId")Long productId,HttpServletRequest request){
        //logger.info("productDetailInfo() method begin...");

        request.setAttribute("productId", productId);
        
        //logger.info("productDetailInfo() method end...");
        
        return "/product/productDetail.jsp";
    }
    
    @RequestMapping(value="/detail",method=RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> show(Product product){
        //logger.info("show() method begin...");

        Map<String,Object> result = new HashMap<String,Object>();
        
        try {
            product = this.productService.read(product.getId());
            
            result.put("product", product);
            result.put("result", Constants.SUCCESS_RESULT_FLAG);
        } catch (Exception e) {
            //logger.error("show() method error...",e);
            
            result.put("result", Constants.FAILURE_RESULT_FLAG);
        }
        
        //logger.info("show() method end...");

        return result;
    }
}
